﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Net;

namespace HostAssistantCore
{
    class IISInfoUtil
    {
        //字串保存系统信息
        private static string sysinfo;

        /// <summary>
        /// 获取系统信息
        /// </summary>
        public string geInfo()
        {
            GetOSVersion();//获取OS版本信息
            GetIISMajorVersion();//获取iis版本信息
            FtpCheck();//查看FTP状态
            GetNetVersion();//获取.Net版本
            return sysinfo;
        }
        /// <summary>
        /// 获取IIS版本
        /// </summary>
        /// <returns></returns>
        private void GetIISMajorVersion()
        {
            double majorVersion = 0;
            double minorVersion = 0;

            try
            {
                var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\INetStp");
                if (key != null)
                {
                    majorVersion = Convert.ToInt32(key.GetValue("MajorVersion"));
                    minorVersion = Convert.ToInt32(key.GetValue("MinorVersion"));
                }
            }
            catch { }

            sysinfo += "[IIS版本:" + majorVersion + "." + minorVersion + "] ";

        }
        /// <summary>
        /// 获取IIS版本静态方法
        /// </summary>
        public static string SGetIISMajorVersion()
        {
            double majorVersion = 0;

            try
            {
                var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\INetStp");
                if (key != null)
                    majorVersion = Convert.ToDouble(key.GetValue("MajorVersion"));
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                return "";
            }

            int left = Convert.ToInt32(majorVersion);

            int right = Convert.ToInt32((majorVersion - left) * 10);

            return Convert.ToString(left) + "." + Convert.ToString(right);
        }
        /// <summary>
        /// 获取系统版本
        /// </summary>
        private void GetOSVersion()
        {
            Version ver = System.Environment.OSVersion.Version;
            string type = Environment.Is64BitOperatingSystem ? "64" : "32";
            string tmpversion = ver.ToString().Substring(0, 5);
            string osversion = "";
            if (ver.Major == 5 && ver.Minor == 0)
            {
                osversion = "Win-2000";
                System.Configuration.ConfigurationManager.AppSettings.Set("OS", "03");
            }
            else if (ver.Major == 5 && ver.Minor == 1)
            {
                osversion = "Win-XP";
                System.Configuration.ConfigurationManager.AppSettings.Set("OS", "03");
            }
            else if (ver.Major == 5 && ver.Minor == 2)
            {
                osversion = "Server-2003";
                System.Configuration.ConfigurationManager.AppSettings.Set("OS", "03");
            }
            else if (ver.Major == 6 && ver.Minor == 0)
            {
                osversion = "Server-2008";
            }
            else if (ver.Major == 6 && ver.Minor == 1 && tmpversion != "6.1.7")
            {
                osversion = "Server-2008";
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2.9")
            {
                //osversion = "Win-8";
                osversion = "Server-2012";
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2")
            {
                osversion = "Server-2012";
            }
            else if (ver.Major == 6 && ver.Minor == 3)
            {
                osversion = "Server-2012";
            }
            else if (ver.Major == 10 && ver.Minor == 0)
            {
                osversion = "Win-10";
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2.9")
            {
                osversion = "Win-10";
            }
            else
            {
                osversion = "未知";
            }
            sysinfo += "[OS版本:" + osversion + "-" + type + "] ";
        }
        /// <summary>
        /// 获取Net版本信息
        /// </summary>
        private void GetNetVersion()
        {
            string netversion = Environment.Version.ToString();
            sysinfo += "[ASP.NET版本:" + netversion + "] ";
        }
        /// <summary>
        /// 检查FTP状态
        /// </summary>
        private void FtpCheck()
        {

            string ftpcheck = "未安装";

            try
            {
                var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\INetStp\Components");
                if (key != null)
                {
                    ftpcheck = "已安装";
                }
                else
                {
                    /*
                    * Test 21 port
                    */
                    IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
                    IPEndPoint point = new IPEndPoint(ip, int.Parse("21"));
                    try
                    {
                        System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient();
                        tcp.Connect(point);
                        ftpcheck = "已安装";
                    }
                    catch
                    {
                        ftpcheck = "未安装";
                        System.Configuration.ConfigurationManager.AppSettings.Set("ftpornot", "no");
                    }
                }
            }
            catch
            {
                System.Configuration.ConfigurationManager.AppSettings.Set("ftpornot", "no");
            }

            sysinfo += "[FTP状态:" + ftpcheck + "] ";
        }
        /// <summary>
        /// 判断是否是08系统还是03系统
        /// </summary>
        public static bool GetSysVersionInfo()
        {
            bool flag = false;
            Version ver = System.Environment.OSVersion.Version;
            string type = Environment.Is64BitOperatingSystem ? "64" : "32";
            string tmpversion = ver.ToString().Substring(0, 5);
            if (ver.Major == 6 && ver.Minor == 0)
            {
                flag = true;
            }
            else if (ver.Major == 6 && ver.Minor == 1 && tmpversion != "6.1.7")
            {
                flag = true;
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2.9")
            {
                flag = true;
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2")
            {
                flag = true;
            }
            else if (ver.Major == 6 && ver.Minor == 3)
            {
                flag = true;
            }
            else if (ver.Major == 10 && ver.Minor == 0)
            {
                flag = true;
            }
            else if (ver.Major == 6 && ver.Minor == 2 && tmpversion == "6.2.9")
            {
                flag = true;
            }
            return flag;
        }
    }
}

